/**
 * Kohive.app.MicroBlogController
 * @extends ExtMVC.Controller
 * Manages the addition and removal of bookmarks
 */
Kohive.app.MicroBlogController = Ext.extend(
		Kohive.controllers.KohiveController, {
			name					: 'microblogs',
			title					: Ext.ux.Translator
					.translate('app_micro_blog'),

			constructor				: function() {
				Kohive.app.PhotosController.superclass.constructor.call(this, {
							viewsPackage	: Kohive.views.microblogs
						});

				this.initEvents();

				// TODO: This is a relic of old MVC, remove it once on ExtMVC 0.6 or above
				this.registerAction('index', this.index);
				this.registerAction('refresh', this.onRefresh);
			},

			/**
			 * Renders the index view
			 */
			index					: function() {
				this.indexWindow = this.renderView('index', {
							listeners	: {
								scope					: this,
								create					: this.create,
								'entered-twitter-pin'	: this.onEnteredTwitterPin,
								'turn-off-twitter-sync'	: this.onTurningOffTwitterSync
							}
						}
				);
			},

			/**
			 * Attempts to create a new MicroBlog entry
			 * @param {Object} data A fields object (e.g. {message: 'I am awesome'})
			 * @param {Ext.form.BasicForm} form The BasicForm from which to take values.  If supplied this
			 * will be used instead of the data argument and a fileUpload Ajax request sent
			 */
			create					: function(data, form) {
				var microBlog = new Kohive.models.MicroBlog(data);

				microBlog.save({
							scope	: this,
							// isUpload: true,
							form	: typeof form == 'undefined' ? null : form
									.getEl(),
							url		: this.scopedUrl("microblogs"),
							success	: function(idCard) {
								this.fireEvent('create', microBlog);
								this.onRefresh();
							},
							failure	: function(idCard) {
								this.fireEvent('create-failed', microBlog);
							}
						});
			},

			/**
			 * Authorizes the current user with twitter on the backend, using the entered pin
			 * @param {String} pin The from the user entered pin
			 */
			onEnteredTwitterPin		: function(pin) {
				Ext.Ajax.request({
							url		: '/twitter/auth/' + pin,
							method	: 'POST',
							scope	: this,
							params	: {
								membership_id	: Kohive.OS.getCurrentHive()
										.get('id')
							},
							success	: function(response) {
								this.fireEvent('twitter-authorized');
								this.fireAction('refresh');
							}

						});
			},

			onTurningOffTwitterSync	: function() {
				Ext.Ajax.request({
							url		: '/twitter/turn_off',
							method	: 'POST',
							scope	: this,
							params	: {
								membership_id	: Kohive.OS.getCurrentHive()
										.get('id')
							},
							success	: function(response) {
								this.fireEvent('turned-off-twitter-sync');
							}
						});
			},

			/**
			 * Returns whether the microblogs app is synced with twitter or not
			 */
			isSyncedWithTwitter		: function() {
				if (this.indexWindow) {
					return this.indexWindow.syncWithTwitter;
				}
			},

			onRefresh				: function() {
				if (!this.indexWindow || !this.indexWindow.store)
					return;

				var store = this.indexWindow.store;
				store.load();
				this.indexWindow.showRefreshing();
			},

			/**
			 * Sets up events emitted by this controller
			 */
			initEvents				: function() {
				this.addEvents(
						/**
				 * @event create
				 * Fired after a new Microblog has been successfully created
				 * @param {Kohive.models.MicroBlog} microBlog The newly created microblog
				 */
						'create',

						/**
						 * @event create-failed
						 * Fired if a create attempt failed
						 * @param {Kohive.models.MicroBlog} microBlog The microblog that could not be created
						 */
						'create-failed',

						/**
						 * @event entered-twitter-pin
						 * Fired after user entered pin
						 * @param {String} pin The entered pin
						 */
						'entered-twitter-pin'
				);
			}
		}
);

ExtMVC.OS.getOS().registerController('microblogs',
		Kohive.app.MicroBlogController
);